package com.example.origin.alibaba.bsever.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.example.origin.alibaba.bsever.service.DemoFeign;
import com.delicacy.grape.alibaba.common.dto.User;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RefreshScope
@RestController
public class DemoController {
    @Value("${name:3333}")
    private String name;

    @Resource
    private DemoFeign demoFeign;


    @GetMapping(value = "/echo/{string}")
//    @SentinelResource(value = "getUserId",blockHandler = "getUserIdHandler",blockHandlerClass = DemoController.class)
    public String echo(@PathVariable String string) {
        return name+"："+"Hello Nacos Discovery " + string;
    }


    @GetMapping(value = "/getUser/{userId}")
    public User getUser(@PathVariable("userId") String userId) {
        return demoFeign.getUser(userId);
    }

    @GetMapping(value = "listUser")
    public List<User> listUser() {
        return demoFeign.listUser();
    }


    @GetMapping("getUserId")
    @SentinelResource(value = "getUserId",blockHandler = "getUserIdHandler",blockHandlerClass = DemoController.class)
    public String getUserId(String userId){
        return userId;
    }
    public String getUserIdHandler(String userId){
        return "不好意思，前方拥挤，请您稍后再试";
    }

    @GetMapping("getCityId")
    @SentinelResource(value = "getCityId" , fallback = "resource", fallbackClass = DemoSentinelResourceHandler.class)
    public String getCityId(String cityId){
        return cityId;
    }
}
